<?php

/**
 * sfCaddy actions.
 *
 * @package    blog
 * @subpackage sfCaddy
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sfCaddyActions extends sfActions
{
	
	public function executeCaddy(sfWebRequest $request)
	{
		 if($this->getUser()->isAuthenticated()) {
		 $this->form2 = new ShopCaddyForm();
        

		if ($request->isMethod('post'))
		{
			$this->form2->bind($request->getParameter($this->form2->getName()));

			if ($this->form2->isValid())
			{
				
				
 
	 
				 
	
  $cadd = $this->form2->save();
	
				  
				    $maj = Doctrine_Core::getTable('ShopProduct')->find(array($this->form2->getValue('product_id')));
				 $maj->number = $maj->number - 1;
				  $maj->save();
				 $this->redirect('shop');
	
 
			}
		}
	 }
	}
	
	
	public function executeHistoric(sfWebRequest $request)
  {
	  
	   if($this->getUser()->isAuthenticated()) {
   $this->current_user = $this->getUser()->getId();
   
    $this->shop_historics = Doctrine_Core::getTable('ShopCaddy')->getCaddyByUser($this->getUser()->getId());
  }
  }
	
  public function executeIndex(sfWebRequest $request)
  {
	  
	   if($this->getUser()->isAuthenticated()) {
   $this->current_user = $this->getUser()->getId();
   
    $this->shop_caddys = Doctrine_Core::getTable('ShopCaddy')->getCaddyByUser($this->getUser()->getId());
      
  }
  
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->shop_caddy = Doctrine_Core::getTable('ShopCaddy')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->shop_caddy);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ShopCaddyForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ShopCaddyForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($shop_caddy = Doctrine_Core::getTable('ShopCaddy')->find(array($request->getParameter('id'))), sprintf('Object shop_caddy does not exist (%s).', $request->getParameter('id')));
    $this->form = new ShopCaddyForm($shop_caddy);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($shop_caddy = Doctrine_Core::getTable('ShopCaddy')->find(array($request->getParameter('id'))), sprintf('Object shop_caddy does not exist (%s).', $request->getParameter('id')));
    $this->form = new ShopCaddyForm($shop_caddy);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($shop_caddy = Doctrine_Core::getTable('ShopCaddy')->find(array($request->getParameter('id'))), sprintf('Object shop_caddy does not exist (%s).', $request->getParameter('id')));
    $shop_caddy->delete();

    $this->redirect('sfCaddy/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $shop_caddy = $form->save();

      $this->redirect('sfCaddy/edit?id='.$shop_caddy->getId());
    }
  }
}
